﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.CM.Controllers
{
    public class RechargeController : Controller
    {
        //
        // GET: /CM/Recharge/
        //充值主页面
        public ActionResult RechargeList()
        {
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            this.ViewData["em"] = em;
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            this.ViewData["selfWorkCode"] = selfWorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            this.ViewData["selfCenterID"] = selfCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            this.ViewData["selfStationID"] = selfStationID;         
            UserOperateContext userOperateContext = new UserOperateContext();
            bool Insert = userOperateContext.getGongneng(1259);//判断登录人有“新增”油卡充值权限
            if (Insert)
            {
                this.ViewData["RolePermissionInsert"] = "Insert";//登录人有新增油卡充值权限
            }
            bool Edit = userOperateContext.getGongneng(1316);//判断登录人有无油卡充值修改权限
            if (Edit)
            {
                this.ViewData["RolePermissionEdit"] = "Edit";//登录人有油卡充值修改权限
            }
            this.ViewData["start"] = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd 00:00:00");
            this.ViewData["end"] = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
            this.ViewData["name"] = UserOperateContext.Current.Session_UsrInfo.Name;
            this.ViewData["time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            return View();
        }

        /// <summary>
        /// 查询充值列表信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DataLoad()
        {
            try
            {
                string start = Request.Form["start"].ToString();
                string end = Request.Form["end"].ToString();
                string center = Request.Form["center"].ToString();
                string station = Request.Form["station"].ToString();
                string cardCode = Request.Form["cardCode"].ToString();
                string invoiceNumber = Request.Form["invoiceNumber"].ToString();

                //获取页容量
                int rows = int.Parse(Request.Form["rows"]);
                //获取请求的页码
                int page = int.Parse(Request.Form["page"]);
                //根据登录人取工号所属分站，分中心编码，以及权限大小
                E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
                string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
                string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
               
                var list = new C_OilCardRechargeBLL().GetRechargeList(page, rows, start, end, center, station, cardCode, invoiceNumber
                                                                       , em, selfWorkCode, selfCenterID, selfStationID);
                JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
                //return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
                return j;
            }

            catch (Exception e)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("InfoID", "0");
                dict.Add("InfoMessage", e.Message);
                return this.Json(dict);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult RechargeEdit()
        {
            string sDetailInfo = Request.Form["sRechargeInfo"].ToString();
            C_OilCardRecharge model = JsonHelper.GetJsonInfoBy<C_OilCardRecharge>(sDetailInfo);
            model.FillTime = DateTime.Now;
            int sid = Convert.ToInt32(model.ID);
            //通过sid查询出此条数据原来的所有值
            C_OilCardRecharge oldModel = new C_OilCardRechargeBLL().GetOldRechargeModel(sid);
            //通过卡号取对应的绑定人姓名和工号
            string diverWorkID = new C_OilCardRechargeBLL().GetWorkID(model.CardCode);
            string diverName = new C_OilCardRechargeBLL().GetName(model.CardCode);
            string centerID = new C_OilCardRechargeBLL().GetCenterID(model.CardCode);
            string stationID = new C_OilCardRechargeBLL().GetStationID(model.CardCode);
            model.DriverWorkID = diverWorkID;
            model.DriverName = diverName;
            model.CenterID = Convert.ToInt32(centerID);
            model.StationID = stationID;
            //油卡充值修改后，油卡基础表中卡内余额自动更新
            //通过油卡卡号查出基础表中此卡对应的信息
            C_OilCardInfo info = new C_OilCardRechargeBLL().GetOilCardList(model.CardCode);         
            int res = new C_OilCardRechargeBLL().Modify(model,"CardCode","CenterID","StationID","CardAmount","Remaining", "RechargeTime",
                                                  "RechargeAmount","DriverWorkID","DriverName","FillTime","FillPerson","FPCode", "Remark");
            if (res > 0)
            {
                 //只有修改卡内金额和加油金额时卡内余额才会更新到油卡信息基础表中 2016-09-05 yh
                if (oldModel.Remaining != model.Remaining || oldModel.CardAmount != model.CardAmount || oldModel.RechargeAmount != model.RechargeAmount)
                {
                    info.bak1 = (model.CardAmount).ToString();
                    int ress = new C_OilCardInfoBLL().Modify(info, "bak1");
                }
                return this.JsonResult(Utility.E_JsonResult.OK, "修改成功！", null, null);
            }
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "修改失败", null, null);
        }
        ///// <summary>
        ///// 新增
        ///// </summary>
        ///// <returns></returns>
        [HttpPost]
        public ActionResult RechargeAdd()
        {
            string sDetailInfo = Request.Form["sRechargeInfo"].ToString();
            C_OilCardRecharge info = JsonHelper.GetJsonInfoBy<C_OilCardRecharge>(sDetailInfo);
            string centerID = new C_OilCardRechargeBLL().GetCenterID(info.CardCode);
            string stationID = new C_OilCardRechargeBLL().GetStationID(info.CardCode);
            info.CenterID = Convert.ToInt32(centerID);
            info.StationID = stationID;
            if(info.StationID==null)
            { info.StationID = ""; }
            info.FillTime = DateTime.Now;
            //通过卡号取对应的绑定人工号和姓名
            string diverWorkID = new C_OilCardRechargeBLL().GetWorkID(info.CardCode);
            string diverName = new C_OilCardRechargeBLL().GetName(info.CardCode);           
            info.DriverWorkID = diverWorkID;
            info.DriverName = diverName;
            List<C_OilCardRecharge> list = new C_OilCardRechargeBLL().GetMaxID();
            if (list.Count == 0)
            { info.ID = 1; }
            else
            { info.ID = list[0].ID + 1; }
            //油卡充值后，油卡基础表中卡内余额自动更新
            //通过油卡卡号查出基础表中此卡对应的信息
            C_OilCardInfo model = new C_OilCardRechargeBLL().GetOilCardList(info.CardCode);           
            int res = new C_OilCardRechargeBLL().Add(info);
            if (res > 0)
            {
                model.bak1 = (info.CardAmount).ToString();
                int ress = new C_OilCardInfoBLL().Modify(model, "bak1");
                return this.JsonResult(Utility.E_JsonResult.OK, "充值成功！", null, null);
            }
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "充值失败", null, null);
        }
        //通过选择卡号查出对应的司机工号，姓名，分中心，分站，卡内余额
         [HttpPost]
        public ActionResult GetListByCardCode(string cardCode)
        {
            C_OilCardLink model = new C_OilCardRechargeBLL().GetOilCardLinkList(cardCode);
            return Json(new { model }, "appliction/json", JsonRequestBehavior.AllowGet);
        }
         public ActionResult GetByCardCode(string cardCode)
         {
             C_OilCardInfo model = new C_OilCardRechargeBLL().GetOilCardList(cardCode);
             return Json(new { model }, "appliction/json", JsonRequestBehavior.AllowGet);
         }

    }
}
